package com.jd.basic.mq;

import com.jd.order.domain.OrderPetAcquisition;
import com.jd.order.mapper.OrderPetAcquisitionMapper;
import com.jd.order.service.IOrderPetAcquisitionService;
import io.netty.util.CharsetUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @BelongsProject: rocketmq-test
 * @BelongsPackage: cn.itsource._01_hello
 * @Author: Director
 * @CreateTime: 2022-07-23  17:28
 * @Description: 消息消费者
 * @Version: 1.0
 */
@Slf4j
@Component
@RocketMQMessageListener(topic = "producer-topic",selectorExpression = "*", consumerGroup = "producer-group")
public class Consumer implements RocketMQListener<MessageExt> {

    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;
    @Autowired
    private IOrderPetAcquisitionService orderPetAcquisitionService;

    @Override
    public void onMessage(MessageExt msg) {
        byte[] body = msg.getBody();
        String msg1 = new String(body, CharsetUtil.UTF_8);
        //通过id获取订单，将消费者消息存入订单
        OrderPetAcquisition order = orderPetAcquisitionMapper.loadById(Long.valueOf(msg1));
        if (order.getState()==0){
            orderPetAcquisitionService.cancel(Long.parseLong(msg1));
        }

    }
}